package com.reflect;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 项目名称: java_algorithm
 * @ClassName ReflectTest
 * @Description
 * @Author huipanxing huipanxing@chinasofti.com   @Date 2022/4/29 9:49
 */
public class ReflectTest {
    public static void main(String[] args) throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException, InstantiationException, NoSuchFieldException {

        Class<?> aClass = Class.forName("com.reflect.User");
        User instance = (User) aClass.newInstance();
        Method[] methods = aClass.getMethods();

        Method sayHello = aClass.getMethod("sayHello", null);
        Object invoke = sayHello.invoke(instance, null);
        System.out.println("fff");

        Field name = aClass.getDeclaredField("name");
        name.setAccessible(true);
        name.set(instance,"xiaohuihui");
        System.out.println(instance);

    }
}
